﻿using System;

namespace WB.Mvc.Forms
{
	internal static class StringExtensions
	{
		public static string ToHumanReadable(this string s)
		{
			s = s.Replace('_', ' ').Trim();

			// index of the first lower case character after the initial character 
			int firstLowerIndex = 1;

			while (Char.IsUpper(s[firstLowerIndex]))
				++firstLowerIndex;

			for (int i = firstLowerIndex; i < s.Length; ++i)
			{
				if (s[i] == ' ')
					continue;

				if (Char.IsUpper(s[i]) && s[i - 1] != ' ')
				{
					s = s.Insert(i, " ");
					i++;
				}
			}

			return s;
		}
	}
}